package com.boomsecret.juc.atomic;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wangjinliang on 2019/1/17.
 */
public class AtomicIntegerDemo {

    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(10);
        System.out.println(atomicInteger.getAndIncrement());
        System.out.println(atomicInteger.get());

        System.out.println("-----------------");
        System.out.println(atomicInteger.getAndDecrement());
        System.out.println(atomicInteger.get());

        System.out.println("-----------------");
        System.out.println(atomicInteger.getAndAdd(2));
        System.out.println(atomicInteger.get());

        System.out.println("-----------------");
        System.out.println(atomicInteger.incrementAndGet());
        System.out.println(atomicInteger.get());

        System.out.println("-----------------");
        System.out.println(atomicInteger.decrementAndGet());
        System.out.println(atomicInteger.get());

        System.out.println("-----------------");
        System.out.println(atomicInteger.addAndGet(8));
        System.out.println(atomicInteger.get());

        System.out.println("-----------------");
        // 一定会更新成功的
        int result = atomicInteger.getAndUpdate(operand -> operand + 2);
        System.out.println(result);
        System.out.println(atomicInteger.get());

        System.out.println("-----------------");
        // 一定会更新成功的
        int updateResult = atomicInteger.updateAndGet(operand -> operand + 8);
        System.out.println(updateResult);
        System.out.println(atomicInteger.get());

        System.out.println("-----------------");
        int accumulateResult = atomicInteger.getAndAccumulate(2, (left, right) -> left + right);
        System.out.println(accumulateResult);
        System.out.println(atomicInteger.get());

        System.out.println("-----------------");
        int temp = atomicInteger.accumulateAndGet(8, (left, right) -> left + right);
        System.out.println(temp);
        System.out.println(atomicInteger.get());
    }
}
